package com.alingrad.android.froyoengine.util;

import java.util.LinkedList;
import java.util.Queue;

import com.alingrad.android.froyoengine.AllocationGuard;

public class FPSRecorder extends AllocationGuard {
	private Queue<Long> mFrames;
	private static final int MAX_FRAMES = 60;
	private long mLastTimeMs;
	
	public FPSRecorder() {
		super();
		mFrames = new LinkedList<Long>();
		mLastTimeMs = System.currentTimeMillis();
	}
	
	public void update() {
		final long currentTime = System.currentTimeMillis();
		long delta = currentTime - mLastTimeMs;
		mFrames.add(delta);
		while (mFrames.size() > MAX_FRAMES)
			mFrames.remove();
		mLastTimeMs = currentTime;
	}
	
	public float getAverage() {
		int total = 0;
		int amount = 0;
		for (Long frame : mFrames) { 
			total += frame;
			amount++;
		}
		return total / (float)amount;
	}
	
	
}
